<template>
  <div class="comment-reply">
    <!-- 头部信息 -->
    <van-nav-bar :title="`${comment.reply_count}条回复`">
      <van-icon slot="left" name="cross" @click="$emit('close')"></van-icon>
    </van-nav-bar>

    <!-- 当前评论项 -->
    <comment-item :comment="comment"></comment-item>

    <!-- 所有评论回复 -->
    <van-cell title="所有回复"></van-cell>
      <comment-list
        :source="comment.com_id"
        type="c"
        :list="commentList"
      ></comment-list>

    <!-- 底部区域 -->
    <div class="article-bottom">
      <van-button
        class="comment-btn"
        type="default"
        round
        size="small"
        @click="isPostShow = true"
      >
        写评论
      </van-button>
    </div>

    <!-- 控制发布评论 -->
    <!-- 发布评论 -->
    <van-popup v-model="isPostShow" position="bottom">
      <post-comment
        :target="articleId"
        @post-success="onPostSuccess"
      ></post-comment>
    </van-popup>

    <!-- 评论回复 -->
    <van-popup v-model="isPostShow" position="bottom">
      <post-comment
        :target="comment.com_id"
        :article-id="articleId"
        @post-success="onPostSuccess"
      ></post-comment>
    </van-popup>
  </div>
</template>

<script>
import CommentItem from "./comment-item.vue";
import CommentList from "./comment-list.vue";
import PostComment from "./post-comment.vue";
export default {
  // 组件名称
  name: "CommentReply",
  // 组件参数 接收来自父组件的数据
  props: {
    //查看回复的评论项
    comment: {
      type: Object,
      required: true,
    },
    articleId: {
      type: [Number, String, Object],
      required: true,
    },
  },
  // 局部注册的组件
  components: {
    "comment-item": CommentItem,
    "comment-list": CommentList,
    "post-comment": PostComment,
  },
  // 组件状态值
  data() {
    return {
      isPostShow: false,
      commentList: [], //评论的回复列表
    };
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  created() {},
  mounted() {},
  // 组件方法
  methods: {
    onPostSuccess(comment) {
      //将发布成功的评论放到评论列表的顶部
      this.commentList.unshift(comment);
      //更新评论的回复数量
      this.comment.reply_count++;
      //关闭评论回复的弹出层
      this.isPostShow = false;
    },
  },
};
</script>

<style scoped lang='less'>
.article-bottom {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: space-around;
  align-items: center;
  box-sizing: border-box;
  height: 44px;
  border-top: 1px solid #d8d8d8;
  background-color: #fff;

  .comment-btn {
    width: 200px;
    height: 30px;
    border: 1px solid #eeeeee;
    font-size: 15px;
    line-height: 23px;
    color: #a7a7a7;
  }
}
/deep/ .van-overlay{
  background-color: rgba(0,0,0,.3);
}
</style>